package com.aletto.general_store.ui.popup;

import android.content.Context;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;

import com.aletto.general_store.R;
import com.aletto.general_store.data.entities.WalletAccount;
import com.aletto.general_store.data.ERC20ServiceAbi;
import com.aletto.general_store.utils.LocalConfigStore;
import com.aletto.general_store.utils.ToastUtils;
import com.lxj.xpopup.core.CenterPopupView;
import com.sisyphus.spinach.network.utils.RxUtil;

import org.web3j.crypto.Credentials;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.utils.Convert;

import java.math.BigInteger;
import java.util.function.Consumer;

import androidx.annotation.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class ApprovePopup extends CenterPopupView {

    private View progress, button_confirm;

    private Consumer<String> consumer;

    public ApprovePopup(@NonNull Context context){
        super(context);
    }

    public ApprovePopup(@NonNull Context context, Consumer<String> consumer) {
        super(context);
        this.consumer = consumer;
    }

    @Override
    protected int getImplLayoutId() {

        return R.layout.popup_approve;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        EditText edit_text = findViewById(R.id.edit_text);
        View button_cancel = findViewById(R.id.button_cancel);
        button_confirm = findViewById(R.id.button_confirm);
        progress = findViewById(R.id.progress);

        button_cancel.setOnClickListener(v -> dismiss());
        button_confirm.setOnClickListener(v -> {
            Editable text = edit_text.getText();
            if (text != null) {
                approve(text.toString());
            }
        });
    }


    private void approve(String price) {
        progress.setVisibility(View.VISIBLE);
        button_confirm.setEnabled(false);

        WalletAccount wallet = LocalConfigStore.getInstance().getWallet();
        Observable.create((ObservableOnSubscribe<String>) emitter -> {

                    EthSendTransaction ethSendTransaction = ERC20ServiceAbi.approve(
//                            ERC20ServiceAbi.get(),
//                            BigInteger.valueOf(97).byteValue(),
                            Credentials.create(wallet.getPrivateKey()),
                            ERC20ServiceAbi.MARKET_CONTRACT_ADDRESS,
                            ERC20ServiceAbi.USDT_CONTRACT_ADDRESS,
                            Convert.toWei(price, Convert.Unit.ETHER).toBigInteger()
                    );

                    emitter.onNext(ethSendTransaction.getTransactionHash());
                    emitter.onComplete();
                })
                .compose(RxUtil.transformer())
                .subscribe(new Observer<>() {
                    @Override
                    public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.rxjava3.annotations.NonNull String string) {
                        consumer.accept(price + " USDT");
                    }

                    @Override
                    public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                        ToastUtils.makeText(e.getLocalizedMessage());
                        dismiss();
                    }

                    @Override
                    public void onComplete() {
                        dismiss();
                        button_confirm.setEnabled(true);
                        progress.setVisibility(View.INVISIBLE);
                    }

                });
    }

}
